package com.leo;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TextQuerySortDemo
 * @Description
 * @Author wangss
 * @date 2020.05.20 23:15
 * @Version 1.0
 */
public class TextQuerySortDemo extends Application {

    private int index = 0;
    private String substr = "";

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        // 文本框、查询按钮、排序按钮
        TextField find_tx = new TextField();
        Button find_bt = new Button("查询");
        Button sort_bt = new Button("排序");

        // 水平布局
        HBox hBox = new HBox(10);
        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(find_tx, find_bt, sort_bt);

        // 文本域
        TextArea find_ta = new TextArea();
        find_ta.setFont(Font.font(20d));
        find_ta.setWrapText(true);
        find_ta.setText("asdfghjklzasdfxcvasdf");

        // 垂直布局
        VBox vBox = new VBox(10);
        vBox.setPrefHeight(300d);
        vBox.setPrefWidth(600d);
        vBox.getChildren().addAll(hBox, find_ta);

        anchorPane.getChildren().addAll(vBox);
        AnchorPane.setTopAnchor(vBox, 10.0);
        AnchorPane.setLeftAnchor(vBox, 10.0);

        primaryStage.setScene(scene);
        primaryStage.setTitle("菜单");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        find_bt.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                String text = find_tx.getText();
                // 获取每一行的内容，返回一个数组
//                System.out.println(find_ta.getParagraphs().toString());
                find_ta.getParagraphs().forEach(t -> {
                    // t代表每一行的内容
                    String value = t.toString();
                    String subStr = value.substring(index);
                    if (subStr.contains(text)) {
                        primaryStage.setTitle("查找成功");
                        // 重新获取焦点
                        find_ta.requestFocus();
                        int i = subStr.indexOf(text);
                        int temp = index + i;
                        index = temp + text.length();
                        find_ta.selectRange(temp, index);
                    } else {
                        primaryStage.setTitle("已经查找完成");
                        index = 0;
                    }
                });
            }
        });

        sort_bt.setOnAction(event -> {
            char[] chars = find_ta.getText().toCharArray();
            List<String> list = new ArrayList<>();
            for (char c : chars) {
                list.add(String.valueOf(c));
                System.out.println(c);
            }
            find_ta.clear();
            list.stream().sorted((item1, item2) -> item1.compareTo(item2)).forEach(item -> find_ta.appendText(item));
        });
    }
}
